import {
    separator
} from '@/filters/vueNumeralFilter/filters';
import { getSignatureList, getAgreementList, getTemplateList, getCrmField } from '@/api/admin/agreement';
export default {
    data() {
        return {
            loading: false, // 加载动画
            showDview: false,
            tableHeight: document.documentElement.clientHeight - 270, // 表的高度
            list: [{
                "orgId": "groupd3149f47",
                "putinId": 1, // 主键Id
                "putinPlatform": "客户来源2", // 消耗渠道
                "putinFormDate": "2023-10-11", // 消耗日期
                "putinMoney": 550, // 消耗金额
                "deptId": 9, // 消耗部门Id
                "deptName": "涵品教育集团-河北知好付网络科技有限公司", // 消耗部门名称
                "remark": null, // 备注
                "createUserId": 1, // 创建人Id
                "createUserName": "groupd3149f47超管", // 创建人名称
                "updateUserId": 1, // 更新人Id
                "updateUserName": "groupd3149f47超管", // 更新人名称
                "createTime": "2023-10-11 14:19:44", // 创建时间
                "updateTime": "2023-10-11 16:36:47" // 更新时间
            }],
            fieldList: [],

            // 列表分页数据
            currentPage: 1,
            pageSize: 15,
            pageSizes: [15, 30, 45, 60],
            total: 0,
            moneyPageData: {}, // 列表金额
            sortData: {}, // 字段排序

            rowID: '',
            rowType: '',
            showDview: false
        }
    },
    computed: {
        getMoney() {
            return separator(this.moneyPageData.putinMoneySum || 0);
        },
    },
    mounted() {
        this.getFieldList()
    },
    methods: {
        /** 获取异步数据 */
        getList(options = {}) {
            const params = {};
            this.loading = true;
            /** 排序操作 */
            if (this.sortData.order) {
                params.sortField = this.sortData.prop
                params.order = this.sortData.order == 'ascending' ? 2 : 1
            }

            // 高级晒选数据
            if (this.filterObj && this.filterObj.length > 0) {
                params.searchList = this.filterObj
            }

            // 签章配置
            if (this.crmType === 'signature') {
                getSignatureList({
                    page: this.currentPage,
                    limit: this.pageSize,
                    ...options,
                    ...params
                }).then(res => {
                    if (res && res.code === 0) {
                        this.list = res.data.records;
                        this.total = res.data.total;

                        this.loading = false;
                    }
                }).catch(err => {
                    this.loading = false;
                })
            } else if (this.crmType === 'templateConfig') { // 模板配置
                getTemplateList({
                    page: this.currentPage,
                    limit: this.pageSize,
                    ...options,
                    ...params
                }).then(res => {
                    if (res && res.code === 0) {
                        this.list = res.data.records;
                        this.total = res.data.total;

                        this.loading = false;
                    }
                }).catch(err => {
                    this.loading = false;
                })
            } else if (this.crmType === 'signingRecord') { // 签署记录
                getAgreementList({
                    page: this.currentPage,
                    limit: this.pageSize,
                    ...options,
                    ...params
                }).then(res => {
                    if (res && res.code === 0) {
                        this.list = res.data.list;
                        this.total = res.data.totalRow;
                        this.loading = false;
                    }
                }).catch(err => {
                    this.loading = false;
                })
            }
        },

        /** 列表某一列点击 */
        handleRowClick(row, column, event) {
            if (column.type === 'selection') {
                return // 多选布局不能点击
            }

            if (this.crmType === 'signingRecord') {
                if (column.property === 'customerName') {
                    console.log(row);
                    this.rowID = row.customerId
                    this.rowType = row.customerType == '学员' ? 'customer' : 'vipStudents'
                    this.showDview = true
                } else {
                    this.showDview = false
                }
            }

            if (this.showDview) {
                this.$store.commit('SET_COLLAPSE', this.showDview)
            }
        },

        /** 获取字段 */
        async getFieldList() {
            let data = [];
            if (this.crmType === 'signature') {
                data = [{
                    fieldName: 'enterpriseName',
                    name: '公司名称',
                    id: 1,
                    sort: false,
                    prop: 'enterpriseName'
                },
                {
                    fieldName: 'sealName',
                    name: '签章名称',
                    id: 13,
                    sort: false,
                    prop: 'sealName'
                },
                {
                    fieldName: 'sealNo',
                    name: '签章编号',
                    id: 3,
                    sort: false,
                    prop: 'sealNo'
                },
                {
                    fieldName: 'businessLicenseNum',
                    name: '组织机构代码',
                    id: 9,
                    sort: false,
                    prop: 'businessLicenseNum'
                },
                {
                    fieldName: 'corporationName',
                    name: '法人姓名',
                    id: 2,
                    sort: false,
                    prop: 'corporationName'
                },
                {
                    fieldName: 'corporationCard',
                    name: '法人身份证号',
                    id: 3,
                    sort: false,
                    prop: 'corporationCard'
                },
                {
                    fieldName: 'corporationPhone',
                    name: '签约手机号',
                    id: 3,
                    sort: false,
                    prop: 'corporationPhone'
                },
                {
                    fieldName: 'contactsName',
                    name: '联系人姓名',
                    id: 3,
                    sort: false,
                    prop: 'contactsName'
                },
                {
                    fieldName: 'contactsCard',
                    name: '联系人身份证号',
                    id: 3,
                    sort: false,
                    prop: 'contactsCard'
                },
                {
                    fieldName: 'contactsPhone',
                    name: '联系人手机号',
                    id: 3,
                    sort: false,
                    prop: 'contactsPhone'
                },
                {
                    fieldName: 'createTime',
                    name: '生成时间',
                    id: 3,
                    sort: false,
                    prop: 'createTime',
                    width: '210'
                }]
            } else if (this.crmType === 'templateConfig') {
                data = [{
                    fieldName: 'templateName',
                    name: '模板名称',
                    id: 1,
                    sort: false,
                    prop: 'templateName'
                },
                {
                    fieldName: 'templateNo',
                    name: '模板编号',
                    id: 13,
                    sort: false,
                    prop: 'templateNo'
                },
                {
                    fieldName: 'createTime',
                    name: '生成时间',
                    id: 3,
                    sort: false,
                    prop: 'createTime'
                },
                {
                    fieldName: 'status',
                    name: '模板状态',
                    id: 9,
                    sort: false,
                    prop: 'status'
                },
                {
                    fieldName: 'createUserName',
                    name: '创建人',
                    id: 3,
                    sort: false,
                    prop: 'createUserName'
                },
                {
                    fieldName: 'fun',
                    name: '操作',
                    id: 3,
                    sort: false,
                    prop: 'fun'
                }]
            } else if (this.crmType === "signingRecord") {
                // data = [{
                //     fieldName: 'customerName',
                //     name: '客户姓名',
                //     id: 1,
                //     sort: false,
                //     prop: 'customerName'
                // }, {
                //     fieldName: 'signUserName',
                //     name: '签约人姓名',
                //     id: 1,
                //     sort: false,
                //     prop: 'signUserName'
                // }, {
                //     fieldName: 'telephone',
                //     name: '电话',
                //     id: 1,
                //     sort: true,
                //     prop: 'telephone'
                // }, {
                //     fieldName: 'signStatus',
                //     name: '签署状态',
                //     id: 1,
                //     sort: true,
                //     prop: 'signStatus'
                // }, {
                //     fieldName: 'signTime',
                //     name: '签署时间',
                //     id: 1,
                //     sort: true,
                //     prop: 'signTime'
                // }, {
                //     fieldName: 'createTime',
                //     name: '发起时间',
                //     id: 1,
                //     sort: true,
                //     prop: 'createTime'
                // }, {
                //     fieldName: 'createUserName',
                //     name: '发起人',
                //     id: 1,
                //     sort: true,
                //     prop: 'createUserName'
                // }, {
                //     fieldName: 'createUserDeptName',
                //     name: '发起人所在部门',
                //     id: 1,
                //     sort: true,
                //     prop: 'createUserDeptName'
                // }, {
                //     fieldName: 'ownerUserName',
                //     name: '销售负责人',
                //     id: 1,
                //     sort: true,
                //     prop: 'ownerUserName'
                // }, {
                //     fieldName: 'ownerDeptName',
                //     name: '销售负责人所在部门',
                //     id: 1,
                //     sort: true,
                //     prop: 'ownerDeptName'
                // }, {
                //     fieldName: 'assistantUserName',
                //     name: '助教协作人',
                //     id: 1,
                //     sort: true,
                //     prop: 'assistantUserName'
                // }, {
                //     fieldName: 'fun',
                //     name: '操作',
                //     id: 1,
                //     sort: true,
                //     width: '200px',
                //     prop: 'fun'
                // }]
                const res = await getCrmField();
                if (res) {
                    data = [...res.data, {
                        fieldName: 'fun',
                        name: '操作',
                        id: 1,
                        sort: true,
                        width: '200px',
                        prop: 'fun'
                    }];
                }
            }
            const fieldList = []
            const moneyFields = []
            for (let index = 0; index < data.length; index++) {
                const element = data[index]

                // 金额字段 需要格式化
                if (element.formType === 'floatnumber') {
                    moneyFields.push(element.fieldName || '')
                }
                fieldList.push({
                    prop: element.fieldName,
                    label: element.name,
                    sortId: element.id,
                    width: element.width
                })
            }

            this.moneyFields = moneyFields;
            this.fieldList = fieldList;
            // 获取好字段开始请求数据
            this.getList()
        },

        /** 格式化字段 */
        fieldFormatter(row, column, cellValue) {
            if (this.moneyFields.includes(column.property)) {
                return separator(row[column.property] || 0)
            }
            return row[column.property] === '' || row[column.property] === null ? '--' : row[column.property]
        },

        /** 字段排序 */
        sortChange(column, prop, order) {
            this.currentPage = 1
            this.sortData = column || {}
            this.getList()
        },

        // 更改每页展示数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.getList()
        },
        // 更改当前页数
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        },
    }
}